package org.skyhi.core;

import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import nu.xom.*;

public class XMLProcess {

   private Document doc = null;

   Elements elements = null;
   
   Element root = null;
   
   String fileName = null;

   public XMLProcess(String fileName) {
      // Document doc = null;
      try {
         this.fileName = fileName;
         doc = new Builder().build(fileName);
      } catch (ValidityException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
      } catch (ParsingException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
      } catch (IOException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
      }
      
      root = doc.getRootElement();
      elements = doc.getRootElement().getChildElements();
   }
   
   /**
    * @return
    */
   public AppointmentBook createAppointmentBook() {
      AppointmentBook appBook = new AppointmentBook();

      for (int i = 0; i < elements.size(); i++) {
         Element element = elements.get(i);
         Appointment appointment = new Appointment();
         appointment.setNumber(Integer.parseInt(element.getFirstChildElement(
               "number").getValue()));
         appointment.setName(element.getFirstChildElement("name").getValue());
         appointment.setDate(new AppointmentDate(element.getFirstChildElement("date").getValue()));
         appointment.setStartTime(element.getFirstChildElement("starttime")
               .getValue());
         appointment.setEndTime(element.getFirstChildElement("endtime")
               .getValue());
         appBook.add(appointment);
      }
      
      return appBook;
   }
   
   /**
    * Produce an XML Element from Appointment object
    * 
    * @param app
    * @return
    */
   public Element produceAElement(Appointment app) {
      Element appointment = new Element("appointment");
      Element number = new Element("number");
      number.appendChild(new Integer(app.getNumber()).toString());
      Element name = new Element("name");
      name.appendChild(app.getName());
      Element date = new Element("date");
      date.appendChild(app.getDate().toString());
      Element starttime = new Element("starttime");
      starttime.appendChild(app.getStartTime());
      Element endtime = new Element("endtime");
      endtime.appendChild(app.getEndTime());
      
      appointment.appendChild(number);
      appointment.appendChild(name);
      appointment.appendChild(date);
      appointment.appendChild(starttime);
      appointment.appendChild(endtime);
      
      return appointment;
   }
   
   public Elements getElements() {
      return elements;
   }
   
   public void writeAnAppointment(Appointment app) throws Exception {
      Serializer serializer = new Serializer(new BufferedOutputStream(
            new FileOutputStream(fileName)),"UTF-8");
      root.appendChild(this.produceAElement(app));
//      doc = new Document(root);
      serializer.setIndent(3);
      serializer.write(doc);
      serializer.flush();
   }
   
   public void WriteAppointmentsToXML(AppointmentBook appBook) throws Exception {
      root.removeChildren();
      
      int count = 0;
      for(Appointment appointment: appBook) {
         count++;
         appointment.setNumber(count);
         writeAnAppointment(appointment);
      }
   }
}
